package Entity

import org.hibernate.Session
import org.hibernate.SessionFactory
import org.hibernate.Transaction
import org.hibernate.cfg.Configuration
import org.joda.time.DateTime
import org.junit.After
import org.junit.Before
import org.junit.Test
import java.sql.Timestamp
import javax.rmi.CORBA.Util

@Suppress("DEPRECATION")
/**
 * Created by futao on 2017/9/24.
 */
class t {
    lateinit var configuraion: Configuration
    lateinit var sessionFactory: SessionFactory
    lateinit var session: Session
    lateinit var transaction: Transaction

    @Before
    fun beforTest() {
        configuraion = Configuration().configure()
        sessionFactory = configuraion.buildSessionFactory()
        session = sessionFactory.openSession()
        transaction = session.beginTransaction()
    }

    @After
    fun afterTest() {
        transaction.commit()
        session.close()
        sessionFactory.close()
    }


    @Test
    fun testInit() {
        //加载配置文件
        val configuration = Configuration().configure()
        //创建会话工厂
        val sessionFactory = configuration.buildSessionFactory()
        //创建会话
        val session = sessionFactory.openSession()
//        sessionFactory.currentSession
        //开启事务
        val transaction = session.beginTransaction()

        //crud
//        val f= File("d:"+File.separator+"banner.png")
//        val input=FileInputStream(f)
//        val blob=Hibernate.getLobCreator(session).createBlob(input, input.available().toLong())
//        session.save(Student(5,"熊","男", Timestamp(DateTime.now().millis) ,"上海市小二房","1887978252",blob))

//        session.createCriteria(Student::class.java)
//                .add(Restrictions.eq())


        //region 输出图片
        //        val entity = session.get(Student::class.java, 4) as Student
//        //获得Blob对象
//        val pic= entity.picture
//        //获得照片的输入流
//        val inPut=pic.getBinaryStream()
//        //创建输出流
//        val file = File("d:"+File.separator+"bannerNew.png")
//        //获得输出流
//        val outPut=FileOutputStream(file)
//        //创建缓冲区
//        var buff = ByteArray(inPut.available())
//        inPut.read(buff)
//        outPut.write(buff)
//        inPut.close()
//        outPut.close()
        //endregion


        val entity = Student().apply {
            name = "小赵"
            gender = "未知"
            birthday = Timestamp(DateTime.now().millis)
//            address = "xiaoshanghai"
            tel = "18"
            picture = null
            addressEntity = Address().apply {
                postCode = "3301013"
                phone = "18797811992"
                address = "Shanghai"
            }
        }

        session.save(entity)
        //提交事务
        transaction.commit()
        //关闭会话
        session.close()
        //关闭会话工厂
        sessionFactory.close()
    }

    @Test
    fun testGet() {
        val configuration = Configuration().configure()
        val sessionFactory = configuration.buildSessionFactory()
        val session = sessionFactory.openSession()
        val transaction = session.beginTransaction()

        val entity = session.get(Student::class.java, 1) as Student
        println(entity.addressEntity.address)
        transaction.commit()
        session.close()
        sessionFactory.close()
    }

    @Test
    fun testLoad() {
        //为空则抛出异常
        val entity = session.load(Student::class.java, 2) as Student
        println("beforeLoad")
        //代理对象
        println(entity::class.java.name)
        //持久化对象
        println(entity.name)
        println(entity::class.java.name)
    }
    @Test
    fun testGet2(){
        val entity =session.get(Student::class.java,2) as Student
        println("beforeTest")
        println(entity.name)
    }

    @Test
    fun testUpdate() {
        val entity = session.get(Student::class.java,1) as Student
        entity.name="赵老四"
        session.update(entity)
    }

    @Test
    fun testDelete(){
        val entity = session.delete(session.get(Student::class.java,1))
    }
    @Test
    fun testAdd(){
        session.save(Student().apply {
            name="熊老板1"
            gender="男"
            birthday= Timestamp(DateTime.now().millis)
            tel="188656"
            addressEntity=Address().apply {
                address="江西省南昌市"
                phone="622-68228"
                postCode="330011"
            }
        })

    }


}